﻿using UnityEngine;
using System.Collections;

public class GridManager : MonoBehaviour {

    public GameObject m_view;
    GameObject clone=null;
    Transform m_panelTrs = null;
    Transform m_parentTrs = null;
    Vector3 m_startPos = new Vector3( -340, 220, 0 );
    Vector3 m_verDis = new Vector3( 0, -60, 0 );
    Vector3 m_horDis = new Vector3( 60 , 0, 0 );
    

    int c_row = 10;
	// Use this for initialization
	void Start () {
        FindChild();
        Init();
        UIEventListener.Get(m_panelTrs.FindChild("Center/Btn").gameObject).onClick = (_go) =>
        {
            Begin();
        };
        
	}

    private void Begin()
    {
        for (int i = 0; i < m_parentTrs.childCount; i++)
        {
            var child = m_parentTrs.GetChild(i);
            if (i % 6 == 0)
            {
                child.FindChild("IsPlaneBody").gameObject.SetActive(true);
            }
            if (i % 7 == 0)
            {
                child.FindChild("IsPlaneHead").gameObject.SetActive(true);
            }
        }
    }

    private void Init()
    {
        for (int i = 0; i < c_row * c_row; ++i)
        {
            var go = NGUITools.AddChild(m_parentTrs.gameObject, clone);
            if (go != null)
            {
                go.transform.localPosition = m_startPos + m_horDis * (i % c_row) + m_verDis * (i / c_row);
                go.transform.FindChild("IsPlaneBody").gameObject.SetActive(false);
                go.transform.FindChild("IsPlaneHead").gameObject.SetActive(false);
            }
        }
    }

    private void FindChild()
    {
        m_panelTrs = m_view.transform;

        clone = m_panelTrs.FindChild("Background/Clone").gameObject;
        m_parentTrs = m_panelTrs.FindChild("Background/List");

        clone.transform.localScale = Vector3.zero;
    }
	
	// Update is called once per frame
	void Update () {
	
	}
}
